package com.ilibrary.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.ilibrary.base.MyAjaxAction;
import com.ilibrary.base.MyBaseVO;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.constants.MyMsgConstants;
import com.ilibrary.service.CollectService;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.CollectVO;
import com.ilibrary.web.vo.UserInfoVO;

public class AjaxDoDeleteCollectAction extends MyAjaxAction {

	private CollectService collectService;
	
	public CollectService getCollectService() {
		return collectService;
	}
	public void setCollectService(CollectService collectService) {
		this.collectService = collectService;
	}

	@Override
	protected JSONObject doExecute(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		JSONObject jo = new JSONObject();
		UserInfoVO loginUser = (UserInfoVO) session.getAttribute(MyConstants.ILIBRARY_lOGIN_USER);
		int collectId = Integer.parseInt(request.getParameter("collectId"));
		ResultDto<CollectVO> resultDtoCollectVO = collectService.findById(collectId);
		CollectVO collectVO = (CollectVO) resultDtoCollectVO.getFirstResult();
		if(collectVO == null) {
			jo.put("success", false);
			jo.put("error", resultDtoCollectVO.getMsg());
			return jo;
		}
		if(loginUser == null || !collectVO.getUserId().equals(collectVO.getUserId())) {
			jo.put("success", false);
			jo.put("error", MyMsgConstants.LOGIN_SESSION_INVALID);
			return jo;
		}
		ResultDto<MyBaseVO> resultDto = collectService.deleteById(collectId);
		jo.put("success", true);
		return jo;
	}

}
